<template>
	<section>
		<!--工具条-->
		<el-col :span="24" class="toolbar" style="padding-bottom: 0px;">
			<el-form :inline="true" :model="filters">

				<el-form-item label="通道类型" prop="operatoruid">
					<el-select v-model="filters.operatoruid"   placeholder="请选择通道类型">
						<el-option value="0" label="全部">全部</el-option>
						<el-option
								v-for="item in operas"
								:key="item.operatoruid"
								:label="item.operatorname"
								:value="item.operatoruid">
						</el-option>
					</el-select>
				</el-form-item>
				<el-form-item>
					<el-input v-model="filters.baseName" placeholder="通道名称"></el-input>
				</el-form-item>
				<el-form-item>
					<el-button type="primary" v-on:click="findPageChannel">查询</el-button>
				</el-form-item>
				<el-form-item>
					<el-button type="primary"  @click="handleAdd">新增</el-button>
				</el-form-item>
			</el-form>
		</el-col>

		<!--列表-->
		<el-table :data="channels" highlight-current-row v-loading="listLoading" border max-height="620"   style="width: 100%;">
			<el-table-column type="index" width="80" label="编号">
			</el-table-column>
			<el-table-column prop="operatorsBase.operatorname" label="通道类型"   >
			</el-table-column>
			<el-table-column prop="baseName" label="通道名称" >
			</el-table-column>
			<el-table-column prop="flow" label="包含话费" :formatter="flowFormatter" >
			</el-table-column>
			<el-table-column prop="basePrice" label="基础售价" >
			</el-table-column>
			<el-table-column prop="createtime" label="创建时间" min-width="100"    >
			</el-table-column>
			<el-table-column label="操作" width="150">
				<template scope="scope">
					<el-button size="small" @click="handleEdit(scope.$index, scope.row)">编辑</el-button>
					<!--<el-button type="danger" size="small" @click="handleDel(scope.$index, scope.row)">删除</el-button>-->
				</template>
			</el-table-column>
		</el-table>

		<el-pagination
				@current-change="handleCurrentChange"
				@size-change="handleSizeChange"
				layout="total,sizes,prev, pager, next"
				:total="total">
		</el-pagination>

		<el-dialog title="编辑通道" v-model="editFormVisible" :close-on-click-modal="false">
			<el-form :model="editForm" label-width="80px" :rules="editFormRules" ref="editForm">
				<el-form-item label="通道类型" prop="operatoruid">
					<el-select v-model="editForm.operatoruid"     placeholder="请选择通道类型">
						<el-option
								v-for="item in operas"
								:key="item.operatoruid"
								:label="item.operatorname"
								:value="item.operatoruid">
						</el-option>
					</el-select>
				</el-form-item>

				<el-form-item label="通道名称" prop="baseName">
					<el-input v-model="editForm.baseName"  placeholder="如移动1元话费"></el-input>
				</el-form-item>
				<el-form-item label="包含话费" prop="flow">
					<el-input v-model="editForm.flow" placeholder="话费单位元" ></el-input>
				</el-form-item>
				<el-form-item label="基础售价" prop="basePrice">
					<el-input v-model="editForm.basePrice" placeholder="配置初始售价,不作为产品真实售价"></el-input>
				</el-form-item>
			</el-form>
			<div slot="footer" class="dialog-footer">
				<el-button @click.native="editFormVisible = false">取消</el-button>
				<el-button type="primary" @click.native="editSubmit" :loading="editLoading">提交</el-button>
			</div>
		</el-dialog>

		<el-dialog title="新增通道" v-model="addFormVisible" :close-on-click-modal="false">
			<el-form :model="addForm" label-width="80px" :rules="addFormRules" ref="addForm">
				<el-form-item label="通道类型" prop="operatoruid">
					<el-select v-model="addForm.operatoruid" placeholder="请选择通道类型">
						<el-option
								v-for="item in operas"
								:key="item.operatoruid"
								:label="item.operatorname"
								:value="item.operatoruid">
						</el-option>
					</el-select>
				</el-form-item>
				<el-form-item label="通道名称" prop="baseName">
					<el-input v-model="addForm.baseName" placeholder="如移动10话费"  ></el-input>
				</el-form-item>
				<el-form-item label="包含话费" prop="flow">
					<el-input v-model="addForm.flow" placeholder="话费单位元" ></el-input>
				</el-form-item>
				<el-form-item label="基础售价" prop="basePrice" >
					<el-input v-model="addForm.basePrice" placeholder="配置初始售价,不作为产品真实售价"></el-input>
				</el-form-item>
			</el-form>
			<div slot="footer" class="dialog-footer">
				<el-button @click.native="addFormVisible = false">取消</el-button>
				<el-button type="primary" @click.native="addSubmit" :loading="addLoading">提交</el-button>
			</div>
		</el-dialog>

	</section>
</template>

<script>
    import util from '../../common/js/util'
    //import NProgress from 'nprogress'
    import { findPageChannel,addChannel,updateChannel,getOperatorListPage} from '../../api/api';

    export default {
        data() {
            return {
                filters: {
                    baseName: '',
                    operatoruid:''
                },
                operas: [],
                channels:[],
                total: 0,
                start:0,
                pageSize:10,
                listLoading: false,
                addFormVisible: false,//新增界面是否显示
                addLoading: false,
                addFormRules: {
                    baseName: [
                        { required: true, message: '请输入通道名称', trigger: 'blur' },
                        { min: 1, max: 30, message: '长度在 1 到 20 个字符', trigger: 'blur' }
                    ],
                    operatoruid:[
                        { required: true, message: '请选择通道类型', trigger: 'blur' }
                    ],
                    basePrice:[
                        { required: true, message: '请输入通道基础售价', trigger: 'blur' }
                    ],
                    flow:[
                        { required: true, message: '请输入通道包含话费', trigger: 'blur' }
                    ]
                },
                //新增界面数据
                addForm: {
                    baseName: '',
                    operatoruid: '',
                    basePrice:'',
                    flow:''
                },
                editFormVisible: false,//编辑界面是否显示
                editLoading: false,
                editFormRules: {
                    baseName: [
                        { required: true, message: '请输入通道名称', trigger: 'blur' },
                        { min: 1, max: 30, message: '长度在 1 到 20 个字符', trigger: 'blur' }
                    ],
                    operatoruid:[
                        { required: true, message: '请输入通道类型', trigger: 'blur' }
                    ],
                    basePrice:[
                        { required: true, message: '请输入通道基础售价', trigger: 'blur' }
                    ],
//                    flow:[
//                        { required: true, message: '请输入通道包含流量', trigger: 'blur' }
//                    ]
                },
                //编辑界面数据
                editForm: {
                    baseName: '',
                    operatoruid: '',
                    basePrice:'',
					flow:''
                },
            }
        },
        methods: {
            flowFormatter: function (row, column){
                return  (row.flow+'话费');
			},
            getOperatorListPage() {
                let para = {
                    start: this.start,
                    pageSize:this.pageSize,
                    operatorname: '',
                };
                this.listLoading = true;
                getOperatorListPage(para).then((datas) => {
                    this.listLoading = false;
                    let { message, code, data } = datas;
                    if (code !== "0") {
                        this.$message({
                            message: message,
                            type: 'error'
                        });
                    } else {
                        this.operas = data.result;
                    }
                });
            },
            findPageChannel() {
                let para = {
                    start: this.start,
                    pageSize:this.pageSize,
                    baseName: this.filters.baseName,
                    operatoruid:this.filters.operatoruid == "0" ? "" : this.filters.operatoruid
                };
                this.listLoading = true;
                findPageChannel(para).then((datas) => {
                    this.listLoading = false;
                    let { message, code, data } = datas;
                    if (code !== "0") {
                        this.$message({
                            message: message,
                            type: 'error'
                        });
                    } else if(data && data.result) {
                        this.channels = data.result;
                        this.total = data.totalCount;
                    }
                });
            },
            //显示编辑界面
            handleEdit: function (index, row) {
                this.editFormVisible = true;
                row.operatoruid = row.operatorsBase.operatoruid;
                this.editForm = Object.assign({}, row);
//                this.editForm.operatoruid =
            },
            //编辑
            editSubmit: function () {
                this.$refs.editForm.validate((valid) => {
                    if (valid) {
                        this.$confirm('确认提交吗？', '提示', {}).then(() => {
                            this.editLoading = true;
                            let para = Object.assign({}, this.editForm);
                            updateChannel(para).then((datas) => {
                                this.editLoading = false;
                                let { message, code, data } = datas;
                                if (code !== "0") {
                                    this.$message({
                                        message: message,
                                        type: 'error'
                                    });
                                }

                                this.$refs['editForm'].resetFields();
                                this.editFormVisible = false;
                                this.findPageChannel();
                            });
                        });
                    }
                });
            },
            //显示新增界面
            handleAdd: function () {
                this.addFormVisible = true;
                this.addForm = {
                    baseName: '',
                    operatoruid: '',
                    basePrice:'',
                    flow:''
                };
            },//新增
            addSubmit: function () {
                this.$refs.addForm.validate((valid) => {
                    if (valid) {
                        this.$confirm('确认提交吗？', '提示', {}).then(() => {
                            this.addLoading = true;
                            let para = Object.assign({}, this.addForm);
                            addChannel(para).then((datas) => {
                                this.addLoading = false;
                                let { message, code, data } = datas;
                                if (code !== "0") {
                                    this.$message({
                                        message: message,
                                        type: 'error'
                                    });
                                }
                                this.$refs['addForm'].resetFields();
                                this.addFormVisible = false;
                                this.findPageChannel();
                            }).catch(function (error) {
                                console.log(error);
                                this.addLoading = false;
                            });
                        });
                    }
                });
            }
            ,handleCurrentChange:function(val){
                this.start = val - 1;
                this.findPageChannel();
            },
            handleSizeChange:function(val){
                this.pageSize = val;
                this.findPageChannel();
            }
        },
        mounted() {
            this.getOperatorListPage();
            this.findPageChannel();
        }
    }

</script>

<style scoped>

</style>
